package com.oscar.ehome.view.pageview;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.oscar.ehome.R;
import com.oscar.ehome.base.activity.BasesActivity;
import com.oscar.ehome.bean.pagebean.InsuranceBean;
import com.oscar.ehome.bean.pagebean.InuranceMessageBean;
import com.oscar.ehome.bean.pagebean.WorkMessageBean;
import com.oscar.ehome.presenter.PagePresenter;
import com.oscar.ehome.utils.basethem.TintManager;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.ToastUtil;
import com.oscar.ehome.view.loginview.LoginActivity;
import com.oscar.ehome.view.pageview.view.InsuranceView;
import com.oscar.ehome.view.pageview.view.InuranceListView;

import org.apache.commons.lang.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by juxing
 * author: MengWanhe
 * date: On 2017/8/31
 * 填写工作日志
 */

public class WorkLogActivity extends BasesActivity<InsuranceView, PagePresenter> implements InsuranceView, TextWatcher {

    /**
     * 返回
     */
    @BindView(R.id.title_img_back)
    ImageView titleImgBack;
    /**
     * 返回
     */
    @BindView(R.id.title_layout_left)
    RelativeLayout titleLayoutLeft;
    /**
     * 添加
     */
    @BindView(R.id.home_tv_num)
    TextView home_tv_num;
    /**
     * 添加
     */
    @BindView(R.id.title_right_layput)
    RelativeLayout rightLayout;
    /**
     * 标题
     */
    @BindView(R.id.title_topbar)
    TextView titleTopbar;
    /**
     *
     */
    @BindView(R.id.basetitle_bar)
    RelativeLayout basetitleBar;
    /**
     * 工作日志
     */
    @BindView(R.id.worklog_edtext)
    EditText worklog_edtext;
    /**
     * 提交
     */
    @BindView(R.id.tijiao)
    Button tijiao;

    PagePresenter presenter;
    String token = "";
    String id = "";
    int isok = 0;

    private CharSequence wordNum;//记录输入的字数
    private int selectionStart;
    private int selectionEnd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_worklog);
        ButterKnife.bind(this);
        setColor();
        initView();
        initData();
    }

    @Override
    protected PagePresenter createPresenter() {
        presenter = new PagePresenter(this);
        return presenter;
    }

    /**
     * 改变颜色
     */
    public void setColor() {
        TintManager.setTint(tijiao, Color.parseColor(SystemConfigFactory.getInstance(WorkLogActivity.this).getSystemConfig().getColor()));
        basetitleBar.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(WorkLogActivity.this).getSystemConfig().getColor()));
    }

    /**
     * 初始化
     */
    public void initView() {
        titleImgBack.setImageResource(R.mipmap.fanhui);
        titleImgBack.setVisibility(View.VISIBLE);
        titleTopbar.setText(getString(R.string.log_text));

        id = getIntent().getStringExtra("id");

        token = SystemConfigFactory.getInstance(WorkLogActivity.this).getSystemConfig().getToken();
    }

    /**
     * 加载数据
     */
    public void initData() {
        if (id.equals("")) {
            worklog_edtext.setEnabled(true);
            tijiao.setVisibility(View.VISIBLE);
            rightLayout.setVisibility(View.GONE);
        } else {
            isok = 1;
            worklog_edtext.setEnabled(false);
            tijiao.setVisibility(View.GONE);
            rightLayout.setVisibility(View.VISIBLE);
            home_tv_num.setVisibility(View.VISIBLE);
            home_tv_num.setText("编辑");
            presenter.getWorklogDetial(token, id);
            showLoading();
        }
    }

    @OnClick({R.id.title_layout_left, R.id.tijiao, R.id.title_right_layput})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_layout_left://返回
                finish();
                break;
            case R.id.tijiao://提交
                if (worklog_edtext.getText().toString() != null && StringUtils.isNotEmpty(worklog_edtext.getText().toString())) {
                    presenter.upload_worklog(id, token, worklog_edtext.getText().toString());
                    showLoading();
                } else {
                    ToastUtil.show(this, "请填写日志");
                }
            case R.id.title_right_layput://编辑
                if (isok == 1) {
                    worklog_edtext.setEnabled(true);
                    tijiao.setVisibility(View.VISIBLE);
                    home_tv_num.setText("完成");
                    isok = 2;
                } else if (isok == 2) {
                    home_tv_num.setText("编辑");
                    worklog_edtext.setEnabled(false);
                    isok = 1;
                    tijiao.setVisibility(View.GONE);
                }
                break;
        }
    }

    @Override
    public void upload_mesage(String result) {

    }

    /**
     * 上传日志
     *
     * @param result
     */
    @Override
    public void upload_worklog(String result) {
        dismissLoading();
        ToastUtil.show(this, "上传成功");
        finish();
    }

    @Override
    public void getInsuranceMessage(InuranceMessageBean result) {

    }

    @Override
    public void getWorkMessage(WorkMessageBean result) {
        dismissLoading();
        if (result != null) {
            if (result.getContent() != null) {
                worklog_edtext.setText(result.getContent());
            }
        }
    }

    @Override
    public void erroy() {
        dismissLoading();
        ToastUtil.show(this, getString(R.string.erry));
    }

    @Override
    public void erry(String result) {
        dismissLoading();
        ToastUtil.show(this, result);
        if (result.contains("过期")) {
            getSharedPreferences("appbase", Context.MODE_PRIVATE).edit().clear().commit();
            SystemConfigFactory.getInstance(this).getSystemConfig().SetLogin(false);
            SystemConfigFactory.getInstance(this).getSystemConfig().setIsFirst(true);
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
            finish();
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        wordNum = s;
    }

    @Override
    public void afterTextChanged(Editable s) {
        selectionStart = worklog_edtext.getSelectionStart();
        selectionEnd = worklog_edtext.getSelectionEnd();
        if (wordNum.length() > 500) {
            //删除多余输入的字（不会显示出来）
            s.delete(selectionStart - 1, selectionEnd);
            int tempSelection = selectionEnd;
            worklog_edtext.setText(s);
            worklog_edtext.setSelection(tempSelection);//设置光标在最后
        }
    }
}
